<?php
namespace app\mobile\model;

use think\Request;
/**
 * Class Shop
 * @package app\mobile\model 店铺表
 */
class Shop extends BaseModel
{
    protected $insert=['status'=>3];

    /*
     * 认证数据
     */
    public function index_query()
    {
        $request = Request::instance();
        $data['type'] = ($request->post('person')==1)?1:2;
        $data['number_id'] = ($request->post('person')==1)?$request->post('person_number_id'):$request->post('shop_number_id');
        $data['name'] = ($request->post('person')==1)?$request->post('person_name'):$request->post('shop_name');
        $pic = explode('|',(new Image())->img_upload('file','identity_Card'));
        $data['picUrl'] = $pic[0];
        $data['company_img'] = (count($pic) == 1)?'':$pic[1];
        return $data;
    }
    //店铺名称
    public function getTitleAttr($value)
    {
        return reStrLen($value,10);
    }

    /*
     * 创建店铺数据
     */
    public function create_shop_query()
    {
        $data['title'] = Request::instance()->post('title');
        $image = (new Image())->img_upload('file','createShop');
        $data['banner'] = explode('|',$image)[0];
        $data['logo'] = explode('|',$image)[1];
        return $data;
    }
    /*
     * 店铺和会员订单一对多
     */
    public function memberOrder()
    {
        return $this->hasMany('MemberOrder','shop_id','member_id');
    }
    /*
     * 店铺和商品一对多
     */
    public function shopGoods()
    {
        return $this->hasMany('ShopGoods','member_id','member_id');
    }

    // 商铺表 商品关联

    public function ShopIndex()

    {

        return $this->hasMany('ShopGoods', 'member_id', 'member_id')->where('sun_shop_goods.status', 1)->where(['state' => 1, 'status' => 1])

            ->field('id,thumb_img,member_id,state,status,title,price')->order(['sort' => 'asc'])->limit(4);

    }

    /*
     * 店铺和会员评价远程一对多
     */
    public function shopGoodsEvaluate()
    {
        return $this->hasManyThrough('ShopGoodsEvaluate','ShopGoods','member_id','goods_id','member_id');
    }

    public function getTypeAttr($value)
    {
        $data = ['1'=>'个人','2'=>'公司','3'=>'自营'];
        return $data[$value];
    }

    public function getCollectNumberAttr($value)
    {
        if ($value < 1000){
            return $value;
        }elseif ($value >=1000 && $value < 10000){
            return round($value/1000,0).'千';
        }elseif ($value >=10000){
            return round($value/10000,0).'万';
        }
    }
}